package hoowin.basic.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Encoder {

	public static final String KEY_MD5 = "MD5";
	
	public static byte[] decryptBASE64(String key) throws IOException{  
	    return new BASE64Decoder().decodeBuffer(key);  
	}  
	 
	public static String encryptBASE64(byte[] key) throws IOException{ 
	    return (new BASE64Encoder()).encodeBuffer(key);  
	}  
	
	public static String encryptMD5(String inputStr) throws NoSuchAlgorithmException{
		MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
        md5.update(inputStr.getBytes()); 
        byte b[] = md5.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
        	i = b[offset];
        	if(i<0)i += 256;
        	if(i<16)buf.append("0");
        	buf.append(Integer.toHexString(i));
        }
        return buf.toString();
	}
	
}